﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
using System.Text.RegularExpressions;
namespace BaHuan
{
    public partial class FrmThongTinNhanVien : Form
    {
        private NhanVienMgt nhanvienMgt = new NhanVienMgt();
        private ChucVuMgt chucvuMgt = new ChucVuMgt();
        private ChiNhanhCongTyMgt chinhanctyMgt = new ChiNhanhCongTyMgt();

        private bool IsEdited = false;
        private bool IsEscape = false;

        public FrmThongTinNhanVien()
        {
            InitializeComponent();
        }

        private void FrmThongTinNhanVien_Load(object sender, EventArgs e)
        {
            dgvDanhSachNhanVien.AutoGenerateColumns = false;
            dgvDanhSachNhanVien.DataSource = GetAllNhanVien();

            cbChucVu.DataSource = GetAllChucVu();
            cbChucVu.DisplayMember = "TenChucVu";
            cbChucVu.ValueMember = "Id";

            cbChiNhanhCty.DataSource = GetAllChiNhanhCongTy();
            cbChiNhanhCty.DisplayMember = "TenChiNhanh";
            cbChiNhanhCty.ValueMember = "Id";

            ChangeStatusControl(true, groupBox1);
            btnLuu.Enabled = false;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
            groupBox1.Enabled = false;
        }

        public void BindingToDataGridView()
        {
            dgvDanhSachNhanVien.DataSource = GetAllNhanVien();

        }

        public void ClearControl(Control control)
        {
            foreach (Control c in control.Controls)
            {
                if (c is TextBox)
                {
                    ((TextBox)c).Clear();
                }
                else if (c is ComboBox)
                {
                    switch (c.Name)
                    {
                        case "cbChuVu":
                            cbChucVu.DataSource = GetAllChucVu();
                            cbChucVu.DisplayMember = "TenChucVu";
                            cbChucVu.ValueMember = "Id";

                            break;
                        case "cbChiNhanhCty":
                            cbChiNhanhCty.DataSource = GetAllChiNhanhCongTy();
                            cbChiNhanhCty.DisplayMember = "TenChiNhanh";
                            cbChiNhanhCty.ValueMember = "Id";

                            break;
                    }
                }

                if (c.HasChildren)
                {
                    ClearControl(c);
                }
            }
        }

        public void ChangeStatusControl(bool IsReadOnly, Control control)
        {
            foreach (Control c in control.Controls)
            {
                if (c is TextBox)
                {
                    if (IsReadOnly)
                    {
                        ((TextBox)c).ReadOnly = true;
                    }
                    else
                    {
                        ((TextBox)c).ReadOnly = false;
                    }
                }
                else if (c is ComboBox)
                {
                    if (IsReadOnly)
                    {
                        ((ComboBox)c).Enabled = false;
                    }
                    else
                    {
                        ((ComboBox)c).Enabled = true;
                    }
                }
                else if (c is DateTimePicker)
                {
                    if (IsReadOnly)
                    {
                        ((DateTimePicker)c).Enabled = false;
                    }
                    else
                    {
                        ((DateTimePicker)c).Enabled = true;
                    }
                }
                if (c.HasChildren)
                {
                    ClearControl(c);
                }
            }
        }

        private void dgvDanhSachNhanVien_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                btnLuu.Enabled = false;
                btnCapNhat.Enabled = true;

                ChangeStatusControl(true, this);

                lblId.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[0].Value.ToString();
                txtHoTen.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[1].Value.ToString();
                dtpNgaySinh.Value = DateTime.Parse(dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[2].Value.ToString());
                chkGioiTinh.Checked = (dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[3].Value.ToString() == "1" ? true : false);
                dtpNgayVaoLam.Value = DateTime.Parse(dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[4].Value.ToString());
                txtDiaChiThuongTru.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[5].Value.ToString();
                txtDiaChiTamTru.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[6].Value.ToString();
                txtCMND.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[7].Value.ToString();
                dtpNgayCap.Value = DateTime.Parse(dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[8].Value.ToString());
                txtNoiCap.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[9].Value.ToString();
                txtEmail.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[10].Value.ToString();
                txtDanToc.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[11].Value.ToString();
                txtDienThoaiDiDong.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[12].Value.ToString();
                txtDienThoaiBan.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[13].Value.ToString();
                txtHocVan.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[14].Value.ToString();
                txtTonGiao.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[15].Value.ToString();
                txtUserName.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[16].Value.ToString();
                txtPassword.Text = dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[17].Value.ToString();
                int idChucVu = Convert.ToInt32(dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[18].Value.ToString());
                cbChucVu.SelectedValue = idChucVu;
                int idChiNhanhCTy = Convert.ToInt32(dgvDanhSachNhanVien.Rows[e.RowIndex].Cells[19].Value.ToString());
                cbChiNhanhCty.SelectedValue = idChiNhanhCTy;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtDienThoaiDiDong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !");
            }
        }

        private void txtDienThoaiBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !");
            }
        }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Vui lòng nhập số !");
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (!char.IsLetterOrDigit((char)e.KeyCode) && !char.IsWhiteSpace((char)e.KeyCode) && e.KeyCode != Keys.Back)
            {
                e.Handled = true;
                MessageBox.Show("Mật khẩu không được phép có ký tự đặc biệt");
            }
        }

        private void cbChucVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cbChucVu.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        }

        private void cbChiNhanhCty_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cbChiNhanhCty.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            btnXoa.Enabled = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Bạn có chắc chắn muốn xóa nhân viên này không?", "Xóa nhân viên", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                ResultObj resultObj = new ResultObj();

                NhanVien nhanvien = new NhanVien();
                nhanvien.UserName = txtUserName.Text;
                nhanvien.Password = txtPassword.Text;
                nhanvien.HoTen = txtHoTen.Text;
                nhanvien.GioiTinh = (chkGioiTinh.Checked == true ? 1 : 0);
                nhanvien.NgaySinh = dtpNgaySinh.Value;
                nhanvien.DiaChiThuongTru = txtDiaChiThuongTru.Text;
                nhanvien.DiaChiTamTru = txtDiaChiTamTru.Text;
                nhanvien.CMND = txtCMND.Text;
                nhanvien.NgayCapCMND = dtpNgayCap.Value;
                nhanvien.NoiCapCMND = txtNoiCap.Text;
                nhanvien.DanToc = txtDanToc.Text;
                nhanvien.IdChucVu = Convert.ToInt32(cbChucVu.SelectedValue.ToString());
                nhanvien.IdChiNhanhCTy = Convert.ToInt32(cbChiNhanhCty.SelectedValue.ToString());
                nhanvien.Email = txtEmail.Text;
                nhanvien.DienThoaiDiDong = txtDienThoaiDiDong.Text;
                nhanvien.DienThoaiNha = txtDienThoaiBan.Text;
                nhanvien.HocVan = txtHocVan.Text;
                nhanvien.TonGiao = txtTonGiao.Text;
                nhanvien.NgayVaoLam = dtpNgayVaoLam.Value;
                nhanvien.Id = Convert.ToInt32(lblId.Text);
                nhanvien.IsDelete = 1;

                resultObj = nhanvienMgt.UpdateNhanVien(nhanvien);

                dgvDanhSachNhanVien.DataSource = GetAllNhanVien();

                ClearControl(groupBox1);
                ChangeStatusControl(true, groupBox1);

                btnLuu.Enabled = true;
                btnCapNhat.Enabled = false;
                btnXoa.Enabled = false;
            }
        }   

        private void btnThem_Click(object sender, EventArgs e)
        {
            IsEscape = true;
            IsEdited = false;

            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
            btnLuu.Enabled = true;
            ClearControl(groupBox1);
            ChangeStatusControl(false, groupBox1);
            groupBox1.Enabled = true;
            txtUserName.Select();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text == "" || txtPassword.Text == "" || txtHoTen.Text == "" || txtCMND.Text == "" || txtNoiCap.Text == "" || txtDiaChiThuongTru.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin !","Thông báo");
            }
            else
            {
                if (IsEscape)
                {
                    ResultObj resultObj = new ResultObj();

                    NhanVien nhanvien = new NhanVien();
                    nhanvien.UserName = txtUserName.Text;
                    nhanvien.Password = txtPassword.Text;
                    nhanvien.HoTen = txtHoTen.Text;
                    nhanvien.GioiTinh = (chkGioiTinh.Checked == true ? 1 : 0);
                    nhanvien.NgaySinh = dtpNgaySinh.Value;
                    nhanvien.DiaChiThuongTru = txtDiaChiThuongTru.Text;
                    nhanvien.DiaChiTamTru = txtDiaChiTamTru.Text;
                    nhanvien.CMND = txtCMND.Text;
                    nhanvien.NgayCapCMND = dtpNgayCap.Value;
                    nhanvien.NoiCapCMND = txtNoiCap.Text;
                    nhanvien.DanToc = txtDanToc.Text;
                    nhanvien.IdChucVu = Convert.ToInt32(cbChucVu.SelectedValue.ToString());
                    nhanvien.IdChiNhanhCTy = Convert.ToInt32(cbChiNhanhCty.SelectedValue.ToString());
                    nhanvien.Email = txtEmail.Text;
                    nhanvien.DienThoaiDiDong = txtDienThoaiDiDong.Text;
                    nhanvien.DienThoaiNha = txtDienThoaiBan.Text;
                    nhanvien.HocVan = txtHocVan.Text;
                    nhanvien.TonGiao = txtTonGiao.Text;
                    nhanvien.NgayVaoLam = dtpNgayVaoLam.Value;

                    if (!IsEdited)
                    {
                        resultObj = nhanvienMgt.SaveNewNhanVien(nhanvien);
                        lblId.Text = resultObj.ReturnCode.ToString();
                    }
                    else
                    {
                        nhanvien.Id = Convert.ToInt32(lblId.Text);
                        resultObj = nhanvienMgt.UpdateNhanVien(nhanvien);
                        IsEdited = false;
                    }


                    if (resultObj.ReturnCode > 0)
                    {
                        MessageBox.Show(" Lưu thành công", "Thông báo");
                        FrmThongTinNhanVien_Load(null, null);
                    }
                    else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                    {
                        MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    }
                    else
                    {
                        MessageBox.Show("Thất bại");
                    }


                    
                    dgvDanhSachNhanVien.DataSource = GetAllNhanVien();

                    ClearControl(groupBox1);
                    ChangeStatusControl(true, groupBox1);

                    btnLuu.Enabled = false;
                    btnCapNhat.Enabled = true;
                    btnXoa.Enabled = true;
                }
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            IsEscape = true;
            IsEdited = true;

            btnLuu.Enabled = true;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;

            groupBox1.Enabled = true;
            ChangeStatusControl(false, groupBox1);
            txtUserName.Select();
        }

        public List<NhanVien> GetAllNhanVien()
        {
            ResultObj resultObj = new ResultObj();
            List<NhanVien> lstNhanVien = new List<NhanVien>();

            resultObj = nhanvienMgt.GetAllNhanVien();
            lstNhanVien = (from nv in(List<NhanVien>)resultObj.Result
                               where nv.IsDelete==0
                               select nv).ToList();

            return lstNhanVien;
        }

        public List<ChucVu> GetAllChucVu()
        {
            ResultObj resultObj = new ResultObj();
            List<ChucVu> lstChucVu = new List<ChucVu>();

            resultObj = chucvuMgt.GetAllChucVu();
            lstChucVu = (List<ChucVu>)resultObj.Result;

            return lstChucVu;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();
            List<ChiNhanhCongTy> lstChiNhanhCongTy = new List<ChiNhanhCongTy>();

            resultObj = chinhanctyMgt.GetAllChiNhanhCongTy();
            lstChiNhanhCongTy = (from cn in(List<ChiNhanhCongTy>)resultObj.Result
                                     where cn.IsDelete==0
                                     select cn).ToList();

            return lstChiNhanhCongTy;
        }

        public List<ChucVu> GetChucVuById(ChucVu chucvu)
        {
            ResultObj resultObj = new ResultObj();
            List<ChucVu> lstChucVu = new List<ChucVu>();

            resultObj = chucvuMgt.GetChucVuById(chucvu);
            lstChucVu = (List<ChucVu>)resultObj.Result;

            return lstChucVu;
        }

        public List<ChiNhanhCongTy> GetChiNhanhCongTyById(ChiNhanhCongTy chinhanhcongty)
        {
            ResultObj resultObj = new ResultObj();
            List<ChiNhanhCongTy> lstChiNhanhCongTy = new List<ChiNhanhCongTy>();

            resultObj = chinhanctyMgt.GetChiNhanhCongTyById(chinhanhcongty);
            lstChiNhanhCongTy = (from cn in(List<ChiNhanhCongTy>)resultObj.Result
                                     where cn.IsDelete==0
                                     select cn).ToList();

            return lstChiNhanhCongTy;
        }
    }
}
